package com.noble.meether.mail.ui.friend;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.noble.glamour.bean.center.user.light.UserLight;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.ExBaseAdapter;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.image.ImageLoader;
import com.noble.meether.R;
import com.noble.meether.fragment.rob.util.RobClickListener;

import java.util.List;

/**
 * 好友列表item
 */
public class MyFriendsAdapter extends ExBaseAdapter<UserLight> {

    public MyFriendsAdapter(Context context, List<UserLight> data) {
        super(context, data);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final MyViewHolder holder;
        if (convertView == null) {
            convertView = inflate(R.layout.item_friend_act);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        final UserLight userInfo = getItem(position);
        ImageLoader.loadCircleAvatar(getContext(), userInfo.getAvatar(), holder.iv_avatar);
        holder.tv_name.setText(userInfo.getNickname());
        holder.user_vip.setVisibility(userInfo.isVip() ? View.VISIBLE : View.GONE);

        // 未读角标
        final long uidTo = userInfo.getUidTo();
        holder.rl_item.setOnClickListener(view -> UIShow.showPrivateChatAct(getContext(), uidTo, userInfo.getNickname(), userInfo.getAvatar(), userInfo.isVip()));
        holder.btn_video.setOnClickListener(new RobClickListener() {
            @Override
            public void onRobClick(View v) {
                ModuleMgr.getRtcChatMgr().inviteVideo((Activity) getContext(), uidTo);
            }
        });
        return convertView;
    }

    private class MyViewHolder {
        private ImageView iv_avatar, btn_video, user_vip;
        private TextView tv_name;
        private RelativeLayout rl_item;

        public MyViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            rl_item = convertView.findViewById(R.id.rl_item);
            iv_avatar = convertView.findViewById(R.id.icon_friend);
            tv_name = convertView.findViewById(R.id.user_nick);
            btn_video = convertView.findViewById(R.id.btn_video);
            user_vip = convertView.findViewById(R.id.user_vip);
        }
    }
}
